import java.awt.Graphics2D;
import java.util.LinkedList;


public class Items {
	LinkedList<Item> list;
	ProgBob progbob;
	
	Items(ProgBob progbob){
		list = new LinkedList<Item>();
		this.progbob = progbob;
	}
	
	public boolean isEmpty(){
		return list.isEmpty();
	}
	
	//TODO: what is akey1? consider move shot_bubble to somewhere else
	public void draw(Graphics2D context){
		for (Item item : list)
        {
            int x = (int)(item.x + 3 * Math.sin(ProgBob.PI_2 * progbob.akey1));
            int y = (int)item.y;
            if (4 * item.time_existed < 1 && item.vel_y > 0)
            {
                double t = 4 * item.time_existed;
                int w = (int)(item.bm.getWidth() * t);
                int h = (int)(item.bm.getHeight() * t);
                // BUGBUG: change size
                item.bm.render(context, x - w / 2, y - h / 2);
            }
            else
            {
                item.bm.render(context, x, y);
            }
        }
        if (progbob.shot_bubble.bm != null)
        {
        	progbob.shot_bubble.bm.render(context, (int)progbob.shot_bubble.x, (int)progbob.shot_bubble.y);
        }
	}
	
	public void update (double time){
		//Item dead = null;
        for (Item item : list)
        {
            item.y += time * item.vel_y;
            if (item.y < -21 || item.y > 380 || item.type == ProgBob.ITEM_FREE)
            {
                list.remove(item);
            }
            else
            {
                item.time_existed += time;
                if (item.type == ProgBob.ITEM_TORPEDO || item.type == ProgBob.ITEM_SMROCKS)
                {
                    if (item.type == ProgBob.ITEM_TORPEDO)
                    {
                        item.vel_y -= 400 * time;
                    }
                    for ( ; Math.abs(item.y - item.py) > 4; item.py -= 4)
                    {
                        PartBub partbub = new PartBub(progbob, item.x, item.py, progbob.bm_part_bub[0], progbob.randDouble(1, 2.2));
                        partbub.vx = progbob.randDouble(-10, 10);
                        partbub.vy = progbob.randDouble(-50, -10);
                        partbub.x += progbob.randDouble(-5, 5);
                        progbob.parts.add(partbub);
                    }
                    for (Bubble bubble = progbob.first_bub; bubble != null; bubble = bubble.next)
                    {
                        double dx = item.x - bubble.x;
                        double dy = item.y - bubble.y;
                        if (dx * dx + dy * dy < ProgBob.RAD_BUBBLE * ProgBob.RAD_BUBBLE)
                        {
                            if (item.type == ProgBob.ITEM_TORPEDO)
                            {
                                for (Bubble bubble1 = progbob.first_bub; bubble1 != null; bubble1 = bubble1.next)
                                {
                                	progbob.spawnBiggerBurst(item.x, item.y);
                                    double ddx = item.x - bubble1.x;
                                    double ddy = item.y - bubble1.y;
                                    if (ddx * ddx + ddy * ddy < 4096 && progbob.path_t0 + bubble1.t > 0)
                                    {
                                    	progbob.spawnBurst(bubble1.x, bubble1.y);
                                        if (bubble1.fish_inside)
                                        {
                                        	progbob.fish_to_save--;
                                        	progbob.total_fish_saved++;
                                            Part part = new Part(progbob, bubble1.x, bubble1.y, progbob.bm_smfish, 0.4);
                                            part.vx = progbob.randDouble(-180, -140);
                                            part.vy = progbob.randDouble(-20, 20);
                                            progbob.parts.add(part);
                                        }
                                        if (bubble1.next != null)
                                        {
                                            bubble1.next.shot = true;
                                            bubble1.next.prev = bubble1.prev;
                                        }
                                        if (bubble1.prev != null)
                                        {
                                            bubble1.prev.next = bubble1.next;
                                        }
                                        else
                                        {
                                        	progbob.first_bub = bubble1.next;
                                        }
                                    }
                                }
                                progbob.game.playSound(progbob.snd_explosion);
                                progbob.shoot_time = 0.5;
                            }
                            else if (item.type == ProgBob.ITEM_SMROCKS)
                            {
                            	progbob.spawnBurst(bubble.x, bubble.y);
                                if (bubble.fish_inside)
                                {
                                	progbob.fish_to_save--;
                                	progbob.total_fish_saved++;
                                    Part part = new Part(progbob, bubble.x, bubble.y, progbob.bm_smfish, 0.4);
                                    part.vx = progbob.randDouble(-180, -140);
                                    part.vy = progbob.randDouble(-20, 20);
                                    progbob.parts.add(part);
                                }
                                if (bubble.next != null)
                                {
                                    bubble.next.shot = true;
                                    bubble.next.prev = bubble.prev;
                                }
                                if (bubble.prev != null)
                                {
                                    bubble.prev.next = bubble.next;
                                }
                                else
                                {
                                	progbob.first_bub = bubble.next;
                                }
                                progbob.game.playSound(progbob.snd_pop);
                            }
                            item.type = ProgBob.ITEM_FREE;
                            if (progbob.first_bub == null)
                            {
                                if (progbob.fish_to_save <= 0)
                                {
                                	progbob.level_completed = true;
                                }
                                else
                                {
                                	progbob.first_bub = new Bubble();
                                	progbob.first_bub.t = -progbob.path_t0 - 1;
                                	progbob.first_bub.bm = progbob.getRandomBubble(true);
                                	progbob.first_bub.fish_inside = true;
                                }
                            }
                        }
                    }
                }
                else
                {
                    for ( ; Math.abs(item.y - item.py) > 4; item.py += 4)
                    {
                        PartBub partbub = new PartBub(progbob, item.x, item.py, progbob.bm_part_bub[0], progbob.randDouble(1, 2.2));
                        partbub.vx = progbob.randDouble(-5, 5);
                        partbub.vy = progbob.randDouble(-50, -10);
                        partbub.x += progbob.randDouble(-7, 7);
                        progbob.parts.add(partbub);
                    }
                    if (item.bonus != null && Math.abs(item.x - progbob.bob_x) < 25 && Math.abs(item.y - (progbob.bob_y + 20)) < 38)
                    {
                    	progbob.spawnBurst(item.x, item.y);
                    	progbob.game.playSound(progbob.snd_pick);
                        PartString partstring = new PartString(progbob, item.x, item.y, item.bonus.name, 0.6);
                        partstring.vx = 0;
                        partstring.vy = -30;
                        progbob.parts.add(partstring);
                        progbob.shoot_time = 0.5;
                        item.bonus.act();
                        list.remove(item);
                    }
                }
                //dead = item;
            }
        }
	}
	
	public void deleteItem (Item i){
		list.remove(i);
	}
}
